package com.soulkun.smarthome.ui.main.home.viewmodel

import android.widget.Toast
import androidx.databinding.ObservableArrayList
import androidx.databinding.ObservableBoolean
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.soulkun.smarthome.logic.dto.*
import com.soulkun.smarthome.logic.model.FamilyModel
import com.soulkun.smarthome.logic.model.RoomModel
import com.soulkun.smarthome.logic.model.SpeechRecognitionResultModel
import com.soulkun.smarthome.logic.repository.ApiRepository
import com.soulkun.smarthome.logic.vo.DeviceInfoVO
import com.soulkun.smarthome.logic.vo.FamilyVO
import com.soulkun.smarthome.logic.vo.RoomDeviceVO
import com.soulkun.smarthome.logic.vo.SpeechRecognitionCommandVO
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import soulkun.library.mvvm.adapter.clearAndAddAll
import soulkun.library.utils.logE
import soulkun.library.utils.toast
import java.util.ArrayList

/**
 * Fragment.navGraphViewModels方法产生的ViewModel域不同于普通的viewModels
 * 需要额外的清除数据
 */
class AppMainHomeViewModel : ViewModel() {

    val familyEmptyStatus = ObservableBoolean(true) //默认true为无家庭
    val maskShowStatus = ObservableBoolean(false) // 默认不显示遮罩
    val selectFamilyLiveData = MutableLiveData<FamilyVO?>()
    val selectRoomLiveData = MutableLiveData<RoomModel?>()
    val selectDeviceLiveData = MutableLiveData<DeviceInfoVO?>()

    val familyList = ObservableArrayList<FamilyVO>()
    val roomDeviceList = ObservableArrayList<RoomDeviceVO>()
    val roomDeviceCommandList = ArrayList<RoomDeviceCommandDTO>()

    fun getFamilyNameList(onComplete: () -> Unit, onRoomEmpty: () -> Unit) =
        ApiRepository.getFamilyNameList(this) {
            familyList.clearAndAddAll(it.data.map { familyModel ->
                FamilyVO(
                    familyModel.id!!,
                    familyModel.name!!,
                    familyModel.latitude!!,
                    familyModel.longitude!!
                )
            })
            if (familyList.isNotEmpty()) {
                familyEmptyStatus.set(false)
                selectFamily(selectFamilyLiveData.value ?: familyList[0], onComplete, onRoomEmpty)
            } else {
                familyEmptyStatus.set(true)
                selectFamilyLiveData.value = null
            }
        }

    fun selectFamily(familyVO: FamilyVO, onComplete: () -> Unit, onRoomEmpty: () -> Unit) {
        familyList.forEach {
            if (it.isSelected.get()) {
                it.isSelected.set(false)
            }
        }
        val position = familyList.indexOfLast {
            it.id == familyVO.id
        }
        if (position != -1) {
            familyVO.isSelected.set(true)
            selectFamilyLiveData.value = familyList[position]
            getRoomDeviceList(onComplete, onRoomEmpty)
        } else {
            selectFamilyLiveData.value = null
            "数据异常，请重新选择家庭".toast()
        }
    }

    private fun getRoomDeviceList(onComplete: () -> Unit, onEmpty: () -> Unit) =
        ApiRepository.getRoomDeviceList(
            this,
            FamilyModel(selectFamilyLiveData.value!!.id)
        ) { response ->
            if (response.data.isNotEmpty()) {
                roomDeviceList.clearAndAddAll(response.data.map { roomDeviceDTO ->
                    RoomDeviceVO(roomDeviceDTO.room, ObservableArrayList<DeviceInfoVO>().apply {
                        addAll(roomDeviceDTO.deviceList.map { deviceInfoDTO ->
                            DeviceInfoVO(
                                deviceInfoDTO.id,
                                deviceInfoDTO.name,
                                deviceInfoDTO.productId,
                                deviceInfoDTO.productName,
                                deviceInfoDTO.familyId,
                                deviceInfoDTO.roomId,
                                ObservableBoolean(deviceInfoDTO.online)
                            )
                        })
                    })
                })
                onComplete()
            }else {
                selectRoomLiveData.value = null
                selectDeviceLiveData.value = null
                onEmpty()
            }
        }

    fun getRoomDeviceCommandList(onComplete: () -> Unit, onEmpty: () -> Unit){
        if (selectFamilyLiveData.value != null) {
            ApiRepository.getRoomDeviceCommandList(
                this,
                FamilyModel(id = selectFamilyLiveData.value!!.id)
            ) {
                if (it.data.isEmpty()) {
                    GlobalScope.launch(Dispatchers.Main){
                        "请添加设备后再使用语音识别".toast()
                    }
                }else{
                    roomDeviceCommandList.clear()
                    roomDeviceCommandList.addAll(ArrayList(it.data))
                    onComplete()
                }
            }
        }else{
            onEmpty()
        }
    }

}